﻿package net.wangit.dev.unit;

import net.wangit.jdbc.DB;
import net.wangit.util.Checker;
import net.wangit.view.page.PageCache;
import net.wangit.view.unit.UnitCache;

public class UnitReferenceUtil {

	private static final String SQL_ELEMENT_COMPONENT = "update ELEMENT set r=? where c=80 and r=?";
	private static final String SQL_QUERY = "update UNIT set lqu=? where lqu=?";
	private static final String SQL_PARENT = "update UNIT set pid=? where pid=?";

	public UnitReferenceUtil() {
	}

	public static void update(DB db, String domain, String oldUnitId,
			String newUnitId) throws Exception {
		if (!Checker.isEmpty(oldUnitId) && !oldUnitId.equals(newUnitId)) {
			String values[] = { newUnitId, oldUnitId };
			int types[] = { 2, 2 };
			db.execute(UnitReferenceUtil.SQL_ELEMENT_COMPONENT, values, types);
			db.execute(UnitReferenceUtil.SQL_QUERY, values, types);
			db.execute(UnitReferenceUtil.SQL_PARENT, values, types);
			UnitCache.clearAll(domain);
			PageCache.clearAll(domain);
		}
	}
}
